﻿/*
=======================================================
    UsingWireropeAttr【在用绳附件】
=======================================================
*/

namespace Services
{
    public interface IUsingWireropeAttrMapping : IMapping<UsingWireropeAttr, UsingWireropeAttrModel, UsingWireropeAttrFilterModel>
    { }

    public class UsingWireropeAttrMapping : AutoUsingWireropeAttrMapping, IUsingWireropeAttrMapping
    {
        private readonly ISqlSugarClient _DB;

        public UsingWireropeAttrMapping(ICurrentUser currentUser, ISqlSugarClient dB)
            : base(currentUser)
        {
            _DB = dB;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override UsingWireropeAttrModel Entity2Model(UsingWireropeAttr entity)
        {
            if (entity == null)
            {
                return null;
            }

            UsingWireropeAttrModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/

            model.AttrFileList = new List<FileModel>() { new FileModel { FileName = model.AttrFileName, Url = model.AttrFileUrl } };
            model.UsingWirerope_Code = entity.UsingWirerope_Code;
            model.UsingWirerope_Name = entity.UsingWirerope_Name;
            model.UsingWirerope_DepartmentName = entity.UsingWirerope_DepartmentName;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override UsingWireropeAttr Model2Entity(UsingWireropeAttrModel model)
        {
            UsingWireropeAttr entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/

            if (model.AttrFileList.IsNotEmpty() && model.AttrFileList.Count > 0)
            {
                entity.AttrFileName = model.AttrFileList.First().FileName;
                entity.AttrFileUrl = model.AttrFileList.First().Url;
            }

            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<UsingWireropeAttrModel> Entity2Model(IEnumerable<UsingWireropeAttr> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<UsingWireropeAttr> Model2Entity(IEnumerable<UsingWireropeAttrModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<UsingWireropeAttr> FilterModel2Where(UsingWireropeAttrFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                if (_currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item1 == false)
                {
                    var usingWireropeIDs = _DB.Queryable<UsingWirerope>().Where(x => _currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item2.Contains(x.DepartmentID))
                        .Select(x => x.UsingWireropeID).Distinct().ToList();

                    where = where.And(x => usingWireropeIDs.Contains(x.UsingWireropeID));
                }
            }
            else if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.监管部门)
            {
                var usingWireropeIDs = _DB.Queryable<UsingWirerope>().Where(x => _currentUser.CurrentUserInfo.LoginRegulatorDepartmentIDs.Contains(x.DepartmentID))
                       .Select(x => x.UsingWireropeID).Distinct().ToList();

                where = where.And(x => usingWireropeIDs.Contains(x.UsingWireropeID));
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////附件地址
            //if (!string.IsNullOrEmpty(filterModel._AttrFileUrl))
            //{
            //    where = where.And(x => x.AttrFileUrl.Contains(filterModel._AttrFileUrl));
            //    //where = where.And(x => x.AttrFileUrl == filterModel._AttrFileUrl);
            //}

            ////创建人
            //if (filterModel._CreateUserID.HasValue)
            //{
            //    where = where.And(x => x.CreateUserID == filterModel._CreateUserID.Value);
            //}

            //在用绳名称
            if (!filterModel._UsingWireropeName.IsEmpty())
            {
                var _expr = Expressionable.Create<UsingWirerope>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => x.Name.Contains(filterModel._UsingWireropeName));

                var usingWireropeIDs = _DB.Queryable<UsingWirerope>().Where(_expr.ToExpression()).Select(x => x.UsingWireropeID).Distinct().ToList();
                where = where.And(x => usingWireropeIDs.Contains(x.UsingWireropeID));
            }

            //矿山
            if (filterModel._UsingWireropeDepartmentIDs.IsNotEmpty() && filterModel._UsingWireropeDepartmentIDs.Count > 0)
            {
                var _expr = Expressionable.Create<UsingWirerope>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => filterModel._UsingWireropeDepartmentIDs.Contains(x.DepartmentID));

                var stockIDs = _DB.Queryable<UsingWirerope>().Where(_expr.ToExpression()).Select(x => x.UsingWireropeID).Distinct().ToList();
                where = where.And(x => stockIDs.Contains(x.UsingWireropeID));
            }

            //在用绳编码
            if (!string.IsNullOrEmpty(filterModel._UsingWireropeCode))
            {
                var _expr = Expressionable.Create<UsingWirerope>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => x.Code.Contains(filterModel._UsingWireropeCode));

                var usingWireropeIDs = _DB.Queryable<UsingWirerope>().Where(_expr.ToExpression()).Select(x => x.UsingWireropeID).Distinct().ToList();
                where = where.And(x => usingWireropeIDs.Contains(x.UsingWireropeID));
            }

            return where;
        }
        #endregion
    }
}
